package com.mes.basicdata.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.mes.basicdata.dao.iface.IRoutingDao;
import com.mes.basicdata.domain.Routing;
import com.mes.common.dao.BasicDao;
@SuppressWarnings("all")
@Repository
public class RoutingDao extends BasicDao<Routing> implements IRoutingDao{

	@Override
	public Collection<Routing> find(String hql) {
		// TODO Auto-generated method stub
		return this.query(hql);
	}	
	
	public Routing getDefaultRouting(String partUid, String deptUid){
		Session session = this.getSessionFactory().getCurrentSession();
		List list = null;
		try {
			StringBuffer hql = new StringBuffer("from " + Routing.class.getName()+" where ");
			hql.append("isDefault = 1 and partUid = ? and mastershopUid = ? ");
			Query query = session.createQuery(hql.toString());
			query.setString(0, partUid);
			query.setString(1, deptUid);
			list = query.list();
			if(list.size() > 0){
				return (Routing)list.get(0);
			}else{
				return null;
			}
		} finally {
			session.flush();
		}
	}

	@Override
	public List<Routing> getRoutingByPartNumber(Map<String, Object> params){
		List<Routing> list;
		try {
			String partUid = (String) params.get("partUid");
			list = this.findByProperty("partUid", partUid);
			return list;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<Routing> getRoutingByPartUid(Map<String, Object> params) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
